package com.yxh.controller;



import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.yxh.common.annotation.Authorization;
import com.yxh.util.DateUtils;
import com.yxh.util.PropertiesUtil;
import com.yxh.util.Response;
import com.yxh.util.SnowflakeIdWorker;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

import static com.yxh.util.APIErrorCode.SUCCESS;

import static com.yxh.util.APIErrorCode.UPDATE_ERROR;



/**
 * Created by Sh1nno on 2018/4/26.
 */


@RestController
@RequestMapping(value = "/file")
public class FileController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);
    private Response response;

    public  static Map<String,Object> fileNames=new HashMap<String,Object>();
    @Scope("prototype")
    @PostMapping
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })

    public Response upload(@RequestParam(value = "file", required = false) MultipartFile mf) throws IOException {
       response = Response.returnResponse(SUCCESS);

        response.setData(DateUtils.getDate("yyyyMMdd")+"/"+url(mf,0));
        return response;
    }

    @PostMapping("/doc")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })

    public Response uploaddoc(@RequestParam(value = "file", required = false) MultipartFile mf) throws IOException {
        response = Response.returnResponse(SUCCESS);

        response.setData(DateUtils.getDate("yyyyMMdd")+"/"+url(mf,1));
        return response;
    }

    @Scope("prototype")
    @PostMapping("/uploadFiles")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response upload(@RequestParam(value = "file", required = false) MultipartFile [] mfs) throws IOException {
        response = Response.returnResponse(SUCCESS);
        String paths="";
        try {
            for(MultipartFile mf :mfs){

                paths = DateUtils.getDate("yyyyMMdd")+"/"+url(mf,0) +"," +paths;
            }
            if(StringUtils.isNotEmpty(paths)){
                paths = paths.substring(0,paths.length()-1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        response.setData(paths);
        return response;
    }

    @PostMapping("/docs")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })

    public Response uploaddoc(@RequestParam(value = "file", required = false)MultipartFile [] mfs) throws IOException {
        response = Response.returnResponse(SUCCESS);
       List<Map<String,Object>> paths=new ArrayList<Map<String, Object>>();
        try {
            for(MultipartFile mf :mfs){
                 String[] ss=url(mf,2).split(",");
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("url",DateUtils.getDate("yyyyMMdd")+"/"+ss[0]);
                map.put("name",ss[1]);
                paths.add(map);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        response.setData(paths);
        return response;
    }


    @Scope("prototype")
    @DeleteMapping
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response delete(@RequestParam(value = "path", required = false) String  path){

        try {
            File file=new File(PropertiesUtil.getProperty("uploadPath")+path);
            if(file.exists()){
                file.delete();
                response = Response.returnResponse(SUCCESS);
            }else
                response = Response.returnResponse(UPDATE_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            response = Response.returnResponse(UPDATE_ERROR);
        }

       // response.setData(paths);
        return response;
    }
    public String url(MultipartFile mf,int i) throws IOException {

        String path;
        //实际上传路径
        String fileName = mf.getOriginalFilename();
        System.out.println(fileName + "开始上传...");
        // String DEFAULT_UPLOADDIR = "D:\\opt\\import_video\\";
        String suffix = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf("."));
        path= SnowflakeIdWorker.creatId()+suffix;
        fileNames.put(path,fileName);
        File file=new File(PropertiesUtil.getProperty("uploadPath") + DateUtils.getDate("yyyyMMdd") + "//",path);
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        mf.transferTo(file);
        if(i==1){
            return path+"*"+fileName;
        }
        if(i==2){
            return path+","+fileName;
        }
        return path;
    }





}
